﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tenta_2014_12_14_5
{
    class WordCounter
    {
        HT<WordCount> hash = new HT<WordCount>(19997);

        public void CountWords(string filename)
        {
            string line;
            try
            {
                StreamReader fin = new StreamReader(filename);
                while ((line = fin.ReadLine()) != null)
                {
                    string[] elements = line.Split(' ');
                    foreach (var item in elements)
	                {
		                var n = hash.Search(item);
                        if (n == null)
                            hash.Add(new WordCount(item, 1));
                        else
                        {
                            if (n.Data.Word == item)
                                n.Data.Count++;
                        }
	                }
                }
                hash.Print();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void Search(string word)
        {            
            Console.WriteLine(hash.Search(word).Data.ToString());
        }
    }
}
